package org.tloss.web.editor.database.editors;

import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.tloss.web.editor.model.Database;

public class SchemaEditor extends EditorPart {

	IResourceChangeListener dirtyListener;

	public void setDirtyListener(IResourceChangeListener dirtyListener) {
		this.dirtyListener = dirtyListener;
	}

	Database database = null;

	public void setDatabase(Database database) {
		this.database = database;
		treeViewer.setInput(database);
		treeViewer.refresh();
	}

	TreeViewer treeViewer;

	public void doSave(IProgressMonitor monitor) {

	}

	public void doSaveAs() {

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		if (!(input instanceof IFileEditorInput)) {
			throw new PartInitException("Input must be a file");
		}
		setSite(site);
		setInput(input);
	}

	public boolean isDirty() {
		return false;
	}

	public boolean isSaveAsAllowed() {

		return false;
	}

	public void createPartControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		layout.numColumns = 1;

		Composite treeContainer = new Composite(composite, SWT.BORDER);
		GridData gridData = new GridData();
		treeContainer.setLayoutData(gridData);
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		TreeColumnLayout treeLayout = new TreeColumnLayout();
		treeContainer.setLayout(treeLayout);
		treeViewer = new TreeViewer(treeContainer, SWT.MULTI
				| SWT.FULL_SELECTION);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);
		TreeColumn keyColumn;
		TreeColumn valueColumn;

		keyColumn = new TreeColumn(tree, SWT.NONE);
		keyColumn.setText("Schema");
		treeLayout.setColumnData(keyColumn, new ColumnWeightData(2));

		valueColumn = new TreeColumn(tree, SWT.NONE);
		valueColumn.setText("Catalog");
		treeLayout.setColumnData(valueColumn, new ColumnWeightData(2));

		treeViewer.setContentProvider(new SchemaContentProvider());
		final SchemaLableProvider treeLabelProvider = new SchemaLableProvider();
		treeViewer.setLabelProvider(treeLabelProvider);

		TreeViewerColumn column1 = new TreeViewerColumn(treeViewer, keyColumn);

		column1.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return treeLabelProvider.getColumnText(element, 0);
			}

			public Image getImage(Object element) {

				return treeLabelProvider.getColumnImage(element, 0);
			}
		});

		TreeViewerColumn column2 = new TreeViewerColumn(treeViewer, valueColumn);

		column2.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return treeLabelProvider.getColumnText(element, 1);
			}

			public Image getImage(Object element) {

				return treeLabelProvider.getColumnImage(element, 1);
			}
		});
		final DialogCellEditor schemaDlg = new DialogCellEditor(
				(Composite) treeViewer.getControl()) {

			@Override
			protected Object openDialogBox(Control cellEditorWindow) {
				SchemaListDlg dialog = new SchemaListDlg(
						cellEditorWindow.getShell(), database);
				if (dialog.open() == Window.OK) {
					treeViewer.refresh();
					if (dirtyListener != null) {
						dirtyListener.resourceChanged(null);
					}
				}
				return database.getSchema();
			}
		};
		column2.setEditingSupport(new EditingSupport(treeViewer) {

			protected boolean canEdit(Object element) {
				return true;
			}

			protected CellEditor getCellEditor(Object element) {
				return schemaDlg;
			}

			protected Object getValue(Object element) {
				return treeLabelProvider.getColumnText(element, 1);
			}

			protected void setValue(Object element, Object value) {

			}
		});

		column1.setEditingSupport(new EditingSupport(treeViewer) {

			protected boolean canEdit(Object element) {
				return true;
			}

			protected CellEditor getCellEditor(Object element) {
				return schemaDlg;
			}

			protected Object getValue(Object element) {
				return treeLabelProvider.getColumnText(element, 0);
			}

			protected void setValue(Object element, Object value) {
			}
		});

		getSite().setSelectionProvider(treeViewer);

		treeViewer.setInput(database);

		treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS);

	}

	public void setFocus() {

	}

}
